#-*- coding: utf-8 -*-

#necessary imports
import urllib2
from BeautifulSoup import BeautifulSoup
import re

#Get all the Data from rebides site
class GetData:
    #constructor
    def __init__(self):
        self.rebidesSite = "http://www.rebides.oces.mctes.pt/Rebides"
        pass

    #connect to the site
    #@param url - site you want to connect
    #@return data - return data from url
    def connect_to_site(self, url):
        try:
            file = urllib2.urlopen(url)
        except:
            print "Connection Error! on ", url
            pass
        
        try:
            data = file.read()
        except:
            print "File Error! Can not read the data file"
            pass
        else:
            file.close()
        return data
    
    #get institutions codes
    #@param year - year to search
    #@return typeUniversitys - list of codes
    def get_TypeUniversity_Codes(self, year):
        yearUrl = self.rebidesSite + "0" + str(year)
        data = self.connect_to_site(yearUrl)
        dataPage = BeautifulSoup(data)
        
        typeUniversitys = {}
        codes = dataPage.findAll("a", )
        codes = self.remove_href(codes, typeUniversitys)
        return typeUniversitys

    #get establishments code
    #@param year - year to search
    #@param typecode - Code of institution
    #@return nameUniversitys - universitys codes
    def get_NameUniversity_Codes(self, year, typeCode):
        siteUrl = self.rebidesSite + "0" + str(year)+ "/" + typeCode
        data = self.connect_to_site(siteUrl)
        dataPage = BeautifulSoup(data)
        
        nameUniversitys = {}
        codes = dataPage.findAll("a", )
        codes = self.remove_href(codes, nameUniversitys)
        return nameUniversitys

    #get out all the trash from codes
    #@param list - list to save the cleaned codes
    #@param list2 - were the codes are whit href
    #@return list - list whit cleaned codes
    def remove_href(self, list, list2):
        for x in list:
            href = x["href"]
            contents = x.contents    
            university = contents[0].encode('utf-8')
            list2[ university ] = href
            pass 
        return list

    #get all thecodes from teachers
    #@param year - year to search
    #@param establishment - establishment
    #@param codes - codes from institutions and establichement
    #@param institution - instution
    #@return teacherList - all the information from teacher
    def get_teachers_information(self, year, establishment, codes, instituction):
        nameID = 1
        degreeID = 3
        curseID = 5
        categoryID = 7
        regimeID = 9  
        url = self.rebidesSite + '0' + str(year)+ '/' + codes
        data = self.connect_to_site(url)

        dataPage = BeautifulSoup(data)
        list = dataPage.findAll('tr', )
        teacherList = []

        for x in list:
            name = x.contents[nameID].contents[0]
            
            degre = x.contents[degreeID].contents[0]
            degre = degre.lstrip()
            
            curse = x.contents[curseID].contents[0]
            curse = curse.lstrip()

            category = x.contents[categoryID].contents[0]
            regime = x.contents[regimeID].contents[0]
            
            teacherCode = self.getMoreInformation(x, codes)

            if name != "NOME COMPLETO":
                if name.encode('utf-8') != '&nbsp;':
                    name = name.expandtabs()
                    name = re.sub('\015', '', name)
                    name = name.lstrip()
                    name = name.encode('utf-8')
                
                    category = category.lstrip()
                    category = category.encode('utf-8')

                    regime = regime.lstrip()
                    regime = regime.encode('utf-8')
                    pass

                degre = degre.encode('utf-8')
                curse = curse.encode('utf-8')

                teacherInformation = [ teacherCode, name, degre,
                                   curse, category, regime, year, establishment,
                                   instituction]
                teacherList.append(teacherInformation)
                pass
            pass
        return teacherList

    #get more information from teacher
    #@param dataTeacher - the data from teacher
    #@param teacherCodes - codes of teacher
    #@return teacherCodes - the code of teacher correct
    def getMoreInformation(self, dataTeacher, teacherCodes):
        data = dataTeacher.contents[11].findAll('a')
        for y in data:
            href = y['href']

            searchOn = 'rebid\_m3\.asp\?CodD\='
            href = re.sub(searchOn, '', href)

            searchOn = '\&CodP\={0}'.format(teacherCodes)
            teacherCodes = re.sub(searchOn, '', href)
            pass
        return teacherCodes 

    #clean all codes from any list
    #@param list - list to clean
    #@reatun list - list cleaned
    def do_corect_codes(self, list):
        for x in list:
            list[x] = re.sub("rebid\_m1\.asp\?codr\=", "",list[x])
            pass
        return list
